package com.trailwalker.traceability.activity;

import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.core.activity.Activity;
import com.trailwalker.javafx.core.entity.Bundle;
import com.trailwalker.javafx.core.entity.Intent;
import com.trailwalker.javafx.utils.storage.PreferencesManager;
import com.trailwalker.javafx.utils.style.StyleUtils;
import com.trailwalker.traceability.entitiy.body.AccountRequestBody;
import com.trailwalker.traceability.entitiy.body.LoginResponseBody;
import com.trailwalker.traceability.entitiy.enum_entity.ProcessType;
import com.trailwalker.traceability.service.ApiDataService;
import io.reactivex.functions.Consumer;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;

/**
 * @author Trailwalker
 * @create 2021-11-11 11:24
 */
public class LoginActivity extends BaseActivity {

    private AnchorPane mApContent;
    private Button mBtnLogin;
    private Button mBtnRegister;
    private TextField mTfAccount;
    private TextField mTfPassword;
    private PreferencesManager mPreferencesManager;

    @Override
    protected void afterSuperCreate() {
        initLoad();
        initView();
        initData();
        initListener();
        onReady();
    }

    private void onReady() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundle();
        if (bundle != null) {
            boolean isAutomaticLogin = bundle.getBoolean("isAutomaticLogin", false);
            if (isAutomaticLogin) {
                login();
            }
        }
    }

    private void initListener() {
        if (mBtnRegister != null) {
            mBtnRegister.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton().equals(MouseButton.PRIMARY)) {
                        Intent intent = new Intent(RegisterActivity.class);
                        if (mTfAccount != null) {
                            String account = mTfAccount.getText();
                            if (StringUtils.isValid(account)) {
                                Bundle bundle = new Bundle();
                                bundle.putString("bundle_account", account);
                                intent.setBundle(bundle);
                            }
                        }
                        startActivity(intent);
                    }
                }
            });
        }

        if (mBtnLogin != null) {
            mBtnLogin.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton().equals(MouseButton.PRIMARY)) {
                        if (validate()) {
                            login();
                        }
                    }
                }
            });
        }
    }

    private void login() {
        String account = mTfAccount.getText();
        String password = mTfPassword.getText();
        AccountRequestBody accountRequestBody = new AccountRequestBody();
        accountRequestBody.setUsername(account);
        accountRequestBody.setPassword(password);
        ApiDataService.getInstance().login(mApplication, accountRequestBody, new Consumer<LoginResponseBody>() {
            @Override
            public void accept(LoginResponseBody loginResponseBody) throws Exception {
                if (loginResponseBody != null) {
                    mPreferencesManager.putString("account", account);
                    mPreferencesManager.putString("password", password);
                    mApplication.getGlobalData().putBoolean("isLogin", true);
                    mApplication.getGlobalData().putString("account", account);
                    mApplication.getGlobalData().putString("token", loginResponseBody.getToken());
                    int processTypeIndex = mPreferencesManager.getInt("processTypeIndex", ProcessType.UNKNOWN.getIndex());
                    ProcessType processType = ProcessType.getProcessTypeByIndex(processTypeIndex);
                    Class<? extends Activity> activityClass = mApplication.getActivityClass(processType);
                    if (activityClass == null) {
                        activityClass = MainActivity.class;
                    }
                    Intent intent = new Intent(activityClass);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private boolean validate() {
        String account = mTfAccount.getText();
        if (StringUtils.isEmpty(account)) {
            return false;
        }
        String password = mTfPassword.getText();
        if (StringUtils.isEmpty(password)) {
            return false;
        }
        return true;
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundle();
        if (bundle != null) {
            String bundleAccount = bundle.getString("account", "");
            if (mTfAccount != null && StringUtils.isValid(bundleAccount)) {
                mTfAccount.setText(bundleAccount);
            }
            String bundlePassword = bundle.getString("password", "");
            if (mTfPassword != null && StringUtils.isValid(bundlePassword)) {
                mTfPassword.setText(bundlePassword);
            }
        }
    }

    private void initView() {
        BorderPane bpContent = new BorderPane();
        mApContent.getChildren().add(bpContent);
        mAdaptUtils.setMatchParentAnchorPane(bpContent);

        HBox hbTitle = new HBox();
        bpContent.setTop(hbTitle);
        hbTitle.setAlignment(Pos.CENTER);
        mAdaptUtils.setScaleHeight(hbTitle, 150);

        Label lblTitle = new Label();
        hbTitle.getChildren().add(lblTitle);
        lblTitle.setText("欢迎登录");
        mAdaptUtils.setScaleTextSize(lblTitle, 40);
        StyleUtils.setTextColor(lblTitle, "#169bd5");

        initCenterView(bpContent);
    }

    private void initCenterView(BorderPane bpContent) {
        VBox vbContent = new VBox();
        bpContent.setCenter(vbContent);
        vbContent.setAlignment(Pos.CENTER);
        vbContent.setSpacing(mAdaptUtils.getScale(50));

        GridPane gpForm = new GridPane();
        vbContent.getChildren().add(gpForm);
        gpForm.setAlignment(Pos.CENTER);
        gpForm.setHgap(mAdaptUtils.getScale(20));
        gpForm.setVgap(mAdaptUtils.getScale(30));
        ColumnConstraints column1 = new ColumnConstraints();
        column1.setHalignment(HPos.RIGHT);
        gpForm.getColumnConstraints().add(column1);
        ColumnConstraints column2 = new ColumnConstraints();
        column2.setHalignment(HPos.LEFT);
        gpForm.getColumnConstraints().add(column2);

        Label lblAccount = new Label();
        gpForm.add(lblAccount, 0, 0);
        lblAccount.setText("姓名：");
        StyleUtils.setTextColor(lblAccount, "#000000");
        mAdaptUtils.setScaleTextSize(lblAccount, 30);

        mTfAccount = new TextField();
        gpForm.add(mTfAccount, 1, 0);
        mAdaptUtils.setScaleSize(mTfAccount, 300, 50);
        mAdaptUtils.setScaleTextSize(mTfAccount, 30);

        Label lblPassword = new Label();
        gpForm.add(lblPassword, 0, 1);
        lblPassword.setText("密码：");
        StyleUtils.setTextColor(lblPassword, "#000000");
        mAdaptUtils.setScaleTextSize(lblPassword, 30);

        mTfPassword = new PasswordField();
        gpForm.add(mTfPassword, 1, 1);
        mAdaptUtils.setScaleSize(mTfPassword, 300, 50);
        mAdaptUtils.setScaleTextSize(mTfPassword, 30);

        HBox hbBtnContainer = new HBox();
        vbContent.getChildren().add(hbBtnContainer);
        hbBtnContainer.setAlignment(Pos.CENTER);
        hbBtnContainer.setSpacing(mAdaptUtils.getScale(100));

        mBtnRegister = new Button();
        hbBtnContainer.getChildren().add(mBtnRegister);
        mBtnRegister.setText("注册");
        mBtnRegister.getStyleClass().add("positive_button");
        mAdaptUtils.setScaleTextSize(mBtnRegister, 25);
        mAdaptUtils.setScaleSize(mBtnRegister, 100, 50);
        StyleUtils.setBorderRadius(mBtnRegister, mAdaptUtils.getScale(100), mAdaptUtils.getScale(50), mAdaptUtils.getScale(10));
        mBtnRegister.setCursor(Cursor.HAND);
        StyleUtils.setTextColor(mBtnRegister, "#ffffff");

        mBtnLogin = new Button();
        hbBtnContainer.getChildren().add(mBtnLogin);
        mBtnLogin.setText("登录");
        mBtnLogin.getStyleClass().add("positive_button");
        mAdaptUtils.setScaleTextSize(mBtnLogin, 25);
        mAdaptUtils.setScaleSize(mBtnLogin, 100, 50);
        StyleUtils.setBorderRadius(mBtnLogin, mAdaptUtils.getScale(100), mAdaptUtils.getScale(50), mAdaptUtils.getScale(10));
        mBtnLogin.setCursor(Cursor.HAND);
        StyleUtils.setTextColor(mBtnLogin, "#ffffff");
    }

    private void initLoad() {
        mPreferencesManager = PreferencesManager.getInstance();
    }

    @Override
    protected Node initContentView() {
        mApContent = new AnchorPane();
        return mApContent;
    }

}
